package licode.unisop.log.service.adapter.controller;

import cn.hutool.core.util.StrUtil;
import licode.unisop.client.api.SopPlatformAuth;
import licode.unisop.client.info.SopIdInfo;
import licode.unisop.log.service.adapter.util.ReqAssembler;
import licode.unisop.unilite.result.UniException;
import licode.unisop.unilite.result.UniResultCode;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 定义平台资源操作的相关接口
 *
 * @author WR
 */
public class BaseController {
    @Resource
    protected HttpServletRequest request;

    @Resource
    private SopPlatformAuth sopPlatformAuth;

    protected void checkPlatform() {
        if (!isPlatformValid()) {
            throwBy();
        }
    }

    protected void throwBy() {
        throw new UniException(UniResultCode.FORBIDDEN.code(), "用户没有操作权限");
    }

    protected boolean isPlatformValid() {
        String platformId = ReqAssembler.getPlatformId(request);
        if (StrUtil.isBlank(platformId)) {
            return false;
        }
        return null != sopPlatformAuth.fromId(SopIdInfo.builder().id(platformId).build());
    }
}
